package com.example.userclient.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.userclient.BookRemark
import com.example.userclient.Main.ClientInfoHolder
import com.example.userclient.R
import com.example.userclient.databinding.TalkItemBinding

object MessageAdapter :RecyclerView.Adapter<MessageAdapter.ViewHolder>() {
    var dataSet: MutableList<BookRemark>? = null
    var cur_talk :String? = null

    var cur_talk_book :Long? = null

    fun clean(list:MutableList<BookRemark>?) : MutableList<BookRemark>?{
        if (list?.size == 0){
            Log.i("fatal","list is empty")
            return ArrayList()
        }

        return list?.filter {
//             it.sendFrom != it.sendTo
            (   it.book_id == cur_talk_book &&  it.sendTo == cur_talk && it.sendFrom == ClientInfoHolder.phone)||
                    (   it.book_id == cur_talk_book &&  it.sendTo == ClientInfoHolder.phone && it.sendFrom == cur_talk)
        }?.toMutableList()
    }

    fun renewDataSet(list:MutableList<BookRemark>){

        if (list.size==0){
            return
        }
        if (dataSet !=null){
            var isReadPosition =0

            dataSet!!.forEach {
                if(it.isRead){
                    isReadPosition++
                }else{
                    return@forEach
                }
            }
            notifyItemRangeChanged(isReadPosition,itemCount)

        }

        val historyNum = dataSet?.size?:0

        val renewNum  = list.size
        if (cur_talk !=null&& cur_talk_book !=null){
//            dataSet  = list.filter {
//                it.book_id ==cur_talk_book &&
//                        (it.sendFrom == cur_talk)
//                                || (  it.book_id ==cur_talk_book && it.sendTo == cur_talk )
//            }.toMutableList()
            dataSet = list
        }else{
            Log.i("talk","unexpected")
            dataSet = list
        }

        val diff = renewNum - historyNum
        if (diff>0){
            notifyItemRangeInserted(historyNum,diff)
        }
    }


    class ViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {

        private val mBinding: TalkItemBinding? = DataBindingUtil.bind(view)

        fun changeData(bookRemark: BookRemark) {
            if (mBinding != null) {
                mBinding.msg = bookRemark
                Log.i("MessageAdapter", "changeData")
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.talk_item, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        dataSet?.get(position)?.let {
            holder.changeData(it)
//            if (!it.isRead&&it.sendTo== ClientInfoHolder.phone&&it.sendTo!=it.sendFrom){
//                it.isRead = true
//                ClientInfoHolder.readRemark?.ids?.add(it.id)
//            }
        }
    }

    override fun getItemCount() = dataSet!!.size
}